
//
// 101223 - AH - (Garage) (*) Created.
//

package org.soekrat.gui.swing.forest;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.soekrat.engine.forest.Tree;

/**
 * <p>
 *   Dialog to show comments.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class CommentViewDialog extends JDialog 
{
  private final JTextArea m_noteArea=new JTextArea(4, 25);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public CommentViewDialog(){ buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public CommentViewDialog(JDialog p){ super(p); buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public CommentViewDialog(JFrame p){ super(p); buildGui(); }
  
  private void buildGui()
  {
    setModal(false);
    setTitle("Comment");
    
    Container c=getContentPane();
    
    c.setLayout(new BorderLayout());
    
    m_noteArea.setEditable(false);
    c.add(new JScrollPane(m_noteArea), BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add
    (
      new JButton
      (
        new AbstractAction("Close")
        {  
          public void actionPerformed(ActionEvent e){ dispose(); }
        }
      )
    );
    c.add(buttons, BorderLayout.SOUTH);
    
    pack();
  }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param tree {@link Tree} to show comment for. If {@code null} or does not
   *             have comment then dialog is not shown or disposed of if is
   *             visible. 
   */
  public void showDialog(Tree tree)
  {
    String comment=(tree==null) ? "" : tree.getComment();
    showDialog(comment);
  }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param comment Comment to show. If {@code null} or is the empty 
   *                {@code String} then dialog is not shown or disposed of if is
   *                visible. 
   */
  public void showDialog(String comment)
  {
    comment=(comment==null) ? "" : comment.trim();
    if (comment.length()>0)
    {
      m_noteArea.setText(comment);
      m_noteArea.setCaretPosition(0);
      setVisible(true);
    }
    else
    {
      dispose();
    } 
  }
  
}
